﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using TLECDataLayer;
using System.Collections;

namespace CreateReservations
{
    public partial class About : System.Web.UI.Page
    {
        public int PageNumber
        {
            get
            {
                if (ViewState["PageNumber"] != null)
                {
                    return Convert.ToInt32(ViewState["PageNumber"]);
                }
                else
                {
                    return 0;
                }
            }
            set
            {
                ViewState["PageNumber"] = value;
            }
        }

        private Category selectedCat;

        protected void Page_Load(object sender, EventArgs e)
        {
            LoadData();
        }

        protected string GetCatName()
        {
            return selectedCat.CategoryType;
        }

        private void LoadData()
        {
            categoriesRepeater.DataSource = Categories.GetAllCategories();
            int tmp;
            if (Request.Params["catId"] != null && int.TryParse(Request.Params["catId"], out tmp))
            {
                selectedCat = Categories.GetCategoryById(Convert.ToInt32(Request.Params["catId"]));

                PagedDataSource pItems = new PagedDataSource();
                pItems.DataSource = TLECDataLayer.Events.GetAllUpcomingEventsByCategory(selectedCat.CategoryId).ToList();
                pItems.AllowPaging = true;
                pItems.PageSize = 2;
                pItems.CurrentPageIndex = PageNumber;

                if (pItems.PageCount > 1)
                {
                    rptPages.Visible = true;
                    ArrayList pages = new ArrayList();
                    for (int i = 1; i <= pItems.PageCount; i++)
                    {
                        pages.Add((i).ToString());
                    }
                    rptPages.DataSource = pages;
                    rptPages.DataBind();
                }
                else
                {
                    rptPages.Visible = false;
                }
                eventsRepeater.DataSource = pItems;
                eventsRepeater.DataBind();
            }
            DataBind();
        }

        protected void rptPages_ItemCommand(object source, RepeaterCommandEventArgs e)
        {
            PageNumber = Convert.ToInt32(e.CommandArgument) - 1;
            LoadData();
        }
    }
}
